package drds.server.net.packet;

import java.io.UnsupportedEncodingException;

/**
 * <pre>
 *  Bytes                      Name
 *  -----                      ----
 *  1                          code
 *  4                          statement_id
 *  1                          flags
 *  4                          iteration_count 
 *  (param_count+7)/8          null_bit_map
 *  1                          new_parameter_bound_flag (if new_params_bound == 1:)
 *  n*2                        type of parameters
 *  n                          values for the parameters   
 *  --------------------------------------------------------------------------------
 *  code:                      always COM_EXECUTE
 *  
 *  statement_id:              statement identifier
 *  
 *  flags:                     reserved for future use. In MySQL 4.0, always 0.
 *                             In MySQL 5.0: 
 *                               0: CURSOR_TYPE_NO_CURSOR
 *                               1: CURSOR_TYPE_READ_ONLY
 *                               2: CURSOR_TYPE_FOR_UPDATE
 *                               4: CURSOR_TYPE_SCROLLABLE
 *  
 *  iteration_count:           reserved for future use. Currently always 1.
 *  
 *  null_bit_map:              A bitmap indicating parameters that are NULL.
 *                             Bits are counted from LSB, using as many bytes
 *                             as necessary ((param_count+7)/8)
 *                             i.e. if the first parameter (parameter 0) is NULL, then
 *                             the least significant bit in the first byte will be 1.
 *  
 *  new_parameter_bound_flag:  Contains 1 if this is the first time
 *                             that "execute" has been called, or if
 *                             the parameters have been rebound.
 *  
 *  type:                      Occurs once for each parameter; 
 *                             The highest significant bit of this 16-bit value
 *                             encodes the unsigned property. The other 15 bits
 *                             are reserved for the type (only 8 currently used).
 *                             This block is sent when parameters have been rebound
 *                             or when a prepared statement is executed for the 
 *                             first time.
 * 
 *  values:                    for all non-NULL values, each parameters appends its value
 *                             as described in Row Data Packet: Binary (column values)
 * @see http://dev.mysql.com/doc/internals/en/execute-packet.html
 * </pre>
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class ExecutePacket extends Packet {

	public long statementId;
	public Value[] values;
	protected Statement statement;

	public ExecutePacket(Statement statement) {
		this.statement = statement;
		this.values = new Value[statement.getParametersNumber()];
	}

	public void read(byte[] bytes, String charset) throws UnsupportedEncodingException {
		BytesHolder bytesHolder = new BytesHolder(bytes);
		packetLength = bytesHolder.readInt3();
		packetId = bytesHolder.read();
		statementId = bytesHolder.readLong4();

		// 读取NULL指示器数据
		int parameterCount = values.length;

		for (int i = 0; i < parameterCount; i++) {
			statement.getParametersType()[i] = bytesHolder.readInt2();
		}

		for (int i = 0; i < parameterCount; i++) {
			Value value = new Value();
			value.type = statement.getParametersType()[i];
			BindValue.bindValue(bytesHolder, value, charset);
			values[i] = value;
		}
	}

	@Override
	public int computePacketSize() {

		return 0;
	}

}